package com.meilai.project.controller.web.adMake.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.controller.web.adMake.entity.BillAdMakeSupplierConfirmPrice;
import com.meilai.project.controller.web.adMake.mapper.BillAdMakeSupplierConfirmPriceMapper;
import com.meilai.project.dto.finance.supplier.SupplierMaterialCalDTO;
import com.meilai.project.util.StringChangeUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * <p>
 * 制作单-供应商确认后，留存当时的材质单价 服务实现类
 * </p>
 *
 * @author 张驰
 * @since 2022-08-22
 */
@Service
public class BillAdMakeSupplierConfirmPriceServiceImpl extends ServiceImpl<BillAdMakeSupplierConfirmPriceMapper, BillAdMakeSupplierConfirmPrice> implements BillAdMakeSupplierConfirmPriceService {
    @Override
    public void calTotal(SupplierMaterialCalDTO dto) {
        // 查询单价
        List<BillAdMakeSupplierConfirmPrice> ms = list(Wrappers.<BillAdMakeSupplierConfirmPrice>lambdaQuery()
                .eq(BillAdMakeSupplierConfirmPrice::getConfirm_id, dto.getConfirm_id())
                .eq(BillAdMakeSupplierConfirmPrice::getMaterial_texture_id, dto.getMaterial_texture_id()));
        if (CollectionUtils.isNotEmpty(ms)) {
            BillAdMakeSupplierConfirmPrice temp = ms.get(0);
            String width = dto.getSize_width();
            String height = dto.getSize_height();
            if ((StringChangeUtil.isInteger(width) || StringChangeUtil.isDouble(width))
                    && (StringChangeUtil.isInteger(height) || StringChangeUtil.isDouble(height))
                    && temp.getPrice() != null) {
                dto.setPrice(temp.getPrice());
                BigDecimal total = new BigDecimal(dto.getSize_width())
                        .multiply(new BigDecimal(dto.getSize_height()))
                        .multiply(temp.getPrice())
                        .multiply(new BigDecimal(dto.getNum().toString()))
                        .divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP);
                dto.setTotal(total);
            }
        }
    }
}
